package com.woniu.backparking.jwtUtil;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author HashSong
 * @ClassName: JWTRealm
 * @Description: 领域类
 * @date 2021/4/29 20:51
 * @Version 1.0
 */
public class JWTRealm extends AuthorizingRealm {
    @Override

    public boolean supports(AuthenticationToken token) {

        return token instanceof JWTToken;//判定token类型，如果是JWTToken则允许执行认证

    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String token= (String) authenticationToken.getPrincipal();
        //进行token校验
        //通过校验说明token有效 返回认证信息
        try {
            JwtUtil.validateJWT(token);
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token, token,getName());
            return info;
        } catch (Exception e) {
            //没有通过校验返回null
            e.printStackTrace();
            return null;
        }
    }
}
